//第一次开发时间  "1.0.0(2018-02-22)";//版本

;!function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(global) :
	typeof define === 'function' && define.amd ?  define(function() { return factory(global) }) :
	(global.ws = factory(global));
}(this, (function (win) {
    //var win=windowPRAM;
	var ws=function(wspram)
	{
		
		var _self=this;
		var emfun=function(){};
		_self.version = "1.3.0(2018-08-16)";//版本
		//_self.Webs={};
		var WebSocket=win.WebSocket||win.MozWebSocket||0;
		if (!WebSocket)
		{
			_self.result=0;_self.msg="您的浏览器不支持WebSocket";
			(wspram.objnothing||emfun)(_self.msg);
			return;
		}
		
		var func={};
		var sends=[];//等待发送的信息
		func.WebSocket=new WebSocket(wspram.wsurl||"ws://localhost:8989/test");
		
		_self.send = function(a)
		{
			var state=func.WebSocket.readyState;

			if(state>1){console.log("连接已经失败，状态码为："+state);return false}
			if(state==0)//未连接
			{
				sends.push(a);
			}
			if(state==1)//已经连接成功
			{
			   func.WebSocket.send(a);
			}
			(wspram.WSSend||emfun)(a);
		
		}
		//_self.wsurl=wspram.wsurl;
		func.WebSocket.onopen = function(event)
		{
			   for(var i=0;i<sends.length;i++)
			   {
				  func.WebSocket.send(sends[i]);
				  sends.shift();
			   }
				//console.log("onopen");
				//console.log(event);
				(wspram.WSonOpen||emfun)(event);
		}
		
		func.WebSocket.onmessage = function(event)
		{
				//console.log("onmessage");
				//console.log(event);
				(wspram.WSonMessage||emfun)(event);
		}
		
		func.WebSocket.onclose = function(event)
		{
				//console.log("onclose");
				//console.log(event);
				sends=[];
				(wspram.WSonClose||emfun)(event);
		}
		
		func.WebSocket.onerror = function(event)
		{
				//console.log("onerror");
				//console.log(event);
				sends=[];
				(wspram.WSonError||function(e){
				console.log(e);
				console.log("连接错误");
				})(event);
		}

	
	}
	return ws;

}));